﻿using Microsoft.AspNetCore.Mvc;
using StarsAdmin.Application.Region.Dtos;
using StarsAdmin.Application.Region.Services;
using StarsAdmin.Core.Attributes;
using StarsAdmin.Core.Models;
using StarsAdmin.Web.Core.Conttrollers;
using System.ComponentModel.DataAnnotations;

namespace StarsAdmin.WebApi.Conttrollers
{
    /// <summary>
    /// 区域管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Order(9)]
    public class RegionController : BaseController
    {
        private readonly IRegionService _regionService;

        public RegionController(IRegionService regionService)
        {
            _regionService = regionService;
        }

        /// <summary>
        /// 其他页面获取区域的接口
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<RegionDto>>> GetRegionsAsync()
        {
            return (await _regionService.GetRegionsAsync()).ResultFormat();
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<RegionDto>>> GetAllAsync([FromQuery] BaseQueryInput input)
        {
            return (await _regionService.GetAllAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<Page<RegionDto>>> GetPageAsync([FromQuery] PageQueryInput input)
        {
            return (await _regionService.GetPageAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<RegionDto>> GetByIdAsync([Required] long id)
        {
            return (await _regionService.GetByIdAsync(id)).ResultFormat("没有找到该条数据", 404);
        }

        /// <summary>
        /// 根据Id集合获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<RegionDto>>> GetByIdsAsync([FromQuery] BaseIdsInput input)
        {
            return (await _regionService.GetByIdsAsync(input.Ids)).ResultFormat();
        }

        /// <summary>
        /// 批量导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportAsync(BaseIdsInput input)
        {
            var result = await _regionService.ExportAsync(input.Ids);
            return result != null ? result : NotFound();
        }

        /// <summary>
        /// 批量导入
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> ImportAsync(ImportRegionInput input)
        {
            return (await _regionService.ImportAsync(input.Regions)).ResultFormat("批量导入失败");
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> AddAsync(AddRegionInput input)
        {
            return (await _regionService.AddAsync(input)).ResultFormat("添加失败");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<bool>> UpdateAsync(UpdateRegionInput input)
        {
            return (await _regionService.UpdateAsync(input)).ResultFormat("更新失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> DeleteAsync([Required] long id)
        {
            return (await _regionService.DeleteAsync(id)).ResultFormat("删除失败");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> BatchDeleteAsync(BaseIdsInput input)
        {
            return (await _regionService.BatchDeleteAsync(input.Ids)).ResultFormat("批量删除失败");
        }
    }
}